<?php

namespace app\interfaces\model;

use think\Db;
use think\Request;
use think\Model;

class MerchantMember extends Model
{

    // 设置主表名
    protected $table = 'sun_member';

    // 模型事件
    protected static function init()
    {
        // 新增前
        self::event('before_insert', function ($data) {
            $data->username = Request::instance()->param('username');
            $data->password = mcs(Request::instance()->param('password'));
            $data->create_ip = getIP();
            $data->create_time = time();
        });

        // 新增后
        self::event('after_insert', function ($data) {

            // 创建会员佣金表
            Db::name('member_brokerage')->insert(['member_id' => $data->id]);

            // 创建会员分销表
            Db::name('member_distribution')->insert(['distribution_id' => '1', 'referrals_id' => $data->id, 'hierarchy' => '0,1,', 'level' => '1', 'create_time' => time()]);

        });
    }

}
